package wangchao.tools;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;

public class XmlUtils {

	public static Map<String, Object> Dom2Map(Document doc) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (doc == null)
			return map;
		Element root = doc.getRootElement();
		for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
			Element e = (Element) iterator.next();
			// System.out.println(e.getName());
			List list = e.elements();
			if (list.size() > 0) {
				map.put(e.getName(), Dom2Map(e));
			} else
				map.put(e.getName(), e.getText());
		}
		return map;
	}

	public static Map Dom2Map(Element e) {
		Map map = new HashMap();
		List list = e.elements();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Element iter = (Element) list.get(i);
				List mapList = new ArrayList();

				if (iter.elements().size() > 0) {
					Map m = Dom2Map(iter);
					if (map.get(iter.getName()) != null) {
						Object obj = map.get(iter.getName());
						if (!obj.getClass().getName().equals("java.util.ArrayList")) {
							mapList = new ArrayList();
							mapList.add(obj);
							mapList.add(m);
						}
						if (obj.getClass().getName().equals("java.util.ArrayList")) {
							mapList = (List) obj;
							mapList.add(m);
						}
						map.put(iter.getName(), mapList);
					} else
						map.put(iter.getName(), m);
				} else {
					if (map.get(iter.getName()) != null) {
						Object obj = map.get(iter.getName());
						if (!obj.getClass().getName().equals("java.util.ArrayList")) {
							mapList = new ArrayList();
							mapList.add(obj);
							mapList.add(iter.getText());
						}
						if (obj.getClass().getName().equals("java.util.ArrayList")) {
							mapList = (List) obj;
							mapList.add(iter.getText());
						}
						map.put(iter.getName(), mapList);
					} else
						map.put(iter.getName(), iter.getText());
				}
			}
		} else
			map.put(e.getName(), e.getText());
		return map;
	}
	

	public static void main(String[] args) throws DocumentException {
		String xml = "<tradeMsg><head><version>1.0.0</version><ref>I321102015101400000001</ref><sysCode>I32110</sysCode><busCode>0313001</busCode><tradeSrc>O</tradeSrc><sender>I32000</sender><reciver>I32110</reciver><date>20151014</date><time>162656</time><reSnd>N</reSnd><rst><tradeCode>00</tradeCode><busiCode>0000</busiCode><info></info></rst></head><data><aac003>李红</aac003><aac002>320582198111036464</aac002><aac004>2</aac004><aac058>01</aac058><aac147>320582198111036464</aac147><aae004>JSB009139412</aae004><aae008>320200D156000005A55882C4C04B1612</aae008><aae009>95588</aae009><aac009>320299</aac009><aac010>乐余镇庆丰村第十�?��50�?/aac010><aae007>320201</aae007><aac183>32058272000013</aac183><socialInss><pageNum>1</pageNum><pageSize>50</pageSize><lst>Y</lst><result><SocialInsurance><aae140>310</aae140><aab301>320299</aab301><aac008>1</aac008><aab004>无锡市人力资源社会保障信息中�?/aab004><aac066>24</aac066></SocialInsurance></result></socialInss><errors><pageNum>1</pageNum><pageSize>50</pageSize><lst>Y</lst><result><ErrorDesc><aaa014>20</aaa014><aaa015>您已参保，但系统提示参保险种不全(参加职工医疗未参加职工养�?，请与当地社保机构联系咨询�?如您是农村居民，新型农村合作医疗保险的参保情况，请与当地管理部门确认�?/aaa015></ErrorDesc></result></errors></data></tradeMsg>";
		Document doc = DocumentHelper.parseText(xml);
		Map map = XmlUtils.Dom2Map(doc);
		System.err.println(map);
	}
}
